package leetcode.d1;

import java.util.Arrays;

/**
 * @Author: hang an
 * @Date: 2025/5/15
 * @Description:
 */
public class R3546 {
    public boolean canPartitionGrid(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        long[] row = new long[n];
        long[] col = new long[m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                row[i] += grid[i][j];
                col[j] += grid[i][j];
            }
        }
        boolean res = false;
        return check(row) || check(col);
    }

    private boolean check(long[] arr) {
        long ls = 0;
        long rs = 0;
        for (long i : arr) {
            rs += i;
        }
        for (long i : arr) {
            ls += i;
            rs -= i;
            if (ls == rs) {
                return true;
            }
        }
        return false;
    }
}
